#include "myMap.h"


MyMap::MyMap()
{
	memset(_map, 0, sizeof(MapDataType) * MAX_MAP_DATA_NUM);
	for (int i = 0; i < MAX_MAP_DATA_NUM; i ++)
	{
		_map[i].busy = false;
	}
}

MyMap::~MyMap()
{
}

bool MyMap::checkExist(const char *macAddr) const
{
	for (int i = 0; i < MAX_MAP_DATA_NUM; i ++)
	{
		if (_map[i].busy && strcmp(_map[i].macAddr, macAddr) == 0)
		{
			return true;
		}
	}

	return false;
}

int MyMap::insert(const char *macAddr, const struct sockaddr_in *natAddr)
{
	for (int i = 0; i < MAX_MAP_DATA_NUM; i++)
	{
		if (_map[i].busy == false)
		{
			_map[i].busy = true;
			strcpy(_map[i].macAddr, macAddr);
			_map[i].natAddr = *natAddr;
			return 0;
		}
	}

	return -1;
}

struct sockaddr_in MyMap::get(const char *macAddr)
{
	for (int i = 0; i < MAX_MAP_DATA_NUM; i++)
	{
		if (_map[i].busy && strcmp(_map[i].macAddr, macAddr) == 0)
		{
			return _map[i].natAddr;
		}
	}

	struct sockaddr_in errSock;
	memset(&errSock, 0, sizeof(struct sockaddr_in));
	return errSock;
}
